package com.edu.base.ums.member.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.edu.base.ums.member.domain.ClsMember;
import com.edu.base.ums.member.service.IClsMemberService;
import com.edu.base.ums.student.service.IClsMemberStudentService;
import com.edu.base.ums.umsMember.domain.UmsMember;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员 Controller
 * 
 * @author lzs
 * @date 2021-05-24
 */
@RestController
@RequestMapping("/cls/member")
public class ClsMemberController extends BaseController
{
    @Autowired
    private IClsMemberService clsMemberService;
    
    @Autowired
    private IClsMemberStudentService clsMemberStudentService;

    /**
     * 查询会员列表分页
     */
    @PreAuthorize("@ss.hasPermi('cls:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(ClsMember clsMember)
    {
        startPage();
        List<ClsMember> list = clsMemberService.selectClsMemberList(clsMember);
        return getDataTable(list);
    }

    /**
     * 导出会员 列表
     */
    @PreAuthorize("@ss.hasPermi('cls:member:export')")
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ClsMember clsMember)
    {
        List<ClsMember> list = clsMemberService.selectClsMemberList(clsMember);
        ExcelUtil<ClsMember> util = new ExcelUtil<ClsMember>(ClsMember.class);
        return util.exportExcel(list, "member");
    }

    /**
     * 获取会员 详细信息
     */
    @PreAuthorize("@ss.hasPermi('cls:member:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(clsMemberService.selectClsMemberById(id));
    }

    /**
     * 新增会员

     */
    @PreAuthorize("@ss.hasPermi('cls:member:add')")
    @Log(title = "会员 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClsMember clsMember)
    {
        return toAjax(clsMemberService.insertClsMember(clsMember));
    }

    /**
     * 修改会员

     */
    @PreAuthorize("@ss.hasPermi('cls:member:edit')") 
    @Log(title = "会员 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClsMember clsMember)
    {
        return toAjax(clsMemberService.updateClsMember(clsMember));
    }

    /**
     * 删除会员

     */
    @PreAuthorize("@ss.hasPermi('cls:member:remove')")
    @Log(title = "会员 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(clsMemberService.deleteClsMemberByIds(ids));
    }
    
    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('cls:member:edit')")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeClsMemberStatus")
    public AjaxResult changeClsMemberStatus(@RequestBody  ClsMember clsMember)
    {
        return toAjax(clsMemberService.changeClsMemberStatus(clsMember));
    }
    
    
    /**
     * 登录名或手机号是唯一标识 
     * 
     */
    @PreAuthorize("@ss.hasPermi('cls:member:query')")
    @GetMapping(value = "/getMemberByPhone")
    public AjaxResult getMemberByPhone(String phone)
    {
    	List<ClsMember> list = 	clsMemberService.selectClsMemberByPhone(phone);
    	boolean flag = false;
    	if(CollectionUtils.isNotEmpty(list)) {
    		flag = true;
    	}
        return AjaxResult.success(flag);
    }
    

    /**
     * 会员开通课程

     */
    @PreAuthorize("@ss.hasPermi('cls:member:edit')") 
    @Log(title = "会员开通课程 ", businessType = BusinessType.UPDATE)
    @PutMapping("/saveCourse")
    public AjaxResult saveCourse(@RequestBody ClsMember clsMember)
    {
        return toAjax(clsMemberStudentService.updateClsMemberToCourse(clsMember));
    }
    
    /**
     * 查询会员列表
     */
    @PreAuthorize("@ss.hasPermi('cls:member:list')")
    @GetMapping("/getClsMemberList")
    public AjaxResult getClsMemberList(ClsMember clsMember)
    {
        List<ClsMember> list = clsMemberService.selectClsMemberList(clsMember);
        return AjaxResult.success(list);
    }
}
